<template>
  <!-- <h1>新增销售员账号</h1> -->
  <div class="sales-add">
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="销售头像">
        <!-- 头像上传 -->
        <el-upload
          class="avatar-uploader"
          action="http://localhost:3000/images/upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="form.avatarImg" :src="'http://localhost:3000/temp/'+form.avatarImg" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        <!-- 头像上传 -->
      </el-form-item>
      <el-form-item label="销售姓名">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
         <el-form-item label="销售密码">
        <el-input v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item label="销售年龄">
        <el-input v-model="form.age"></el-input>
      </el-form-item>

      <el-form-item label="销售性别">
        <el-radio-group v-model="form.gender">
          <el-radio label="男"></el-radio>
          <el-radio label="女"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="销售电话">
        <el-input v-model="form.tell"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">新增</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {createNamespacedHelpers} from "vuex";
const {mapActions:SaleMapActions} = createNamespacedHelpers("sales");
export default {
  data(){
    return {
      form:{
        avatarImg:'',
        username:'',
        age:'',
        gender:'',
        tell:'',
      }
    }
  },
  methods:{
    ...SaleMapActions(["addSalesAsync"]),
    //头像上传相关操作
    handleAvatarSuccess(res, file) {
        this.imageUrl = URL.createObjectURL(file.raw);
        // console.log(res);
        if(res.status){
          this.form.avatarImg = res.data;
          // console.log(this.form.avatarImg);
        }
      },
    beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      },
      async onSubmit(){

      const data = await this.addSalesAsync(this.form);
      if(data.status){
        this.$message({
          message: data.message,
          type: 'success'
        });
      this.$router.push('/SalesList');
      }else{
        this.$message.error(data.message);
      }
    },  
  },
}
</script>

<style>
  .sales-add {
  width: 400px;
}
.avatar-uploader .el-upload {
    border: 1px dashed #ffffff;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    background-color: #ffffff;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #ddd9d9;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
</style>